package cn.iocoder.yudao.module.system.dal.mysql.dict;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.data.DictDataExtListReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dict.DictDataExtDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface DictDataExtMapper extends BaseMapper<DictDataExtDO> {

    @Select("SELECT * FROM system_dict_data_ext WHERE parent_id = #{parentId} and status = 0")
    List<DictDataExtDO> selectByParentId(@Param("parentId") Long parentId);

    @Select("SELECT * FROM system_dict_data_ext WHERE value = #{value} LIMIT 1")
    DictDataExtDO selectByValue(@Param("value") String value);

    @Select("SELECT * FROM system_dict_data_ext WHERE id = #{id}")
    DictDataExtDO selectById(@Param("id") Long id);

    default DictDataExtDO selectByDictTypeAndLabel(String dictType, String value) {
        return selectOne(new LambdaQueryWrapper<DictDataExtDO>().eq(DictDataExtDO::getDictType, dictType)
                .eq(DictDataExtDO::getLabel, value));
    }

    default DictDataExtDO selectByDictTypeAndValue(String dictType, String value) {
        return selectOne(new LambdaQueryWrapper<DictDataExtDO>().eq(DictDataExtDO::getDictType, dictType)
                .eq(DictDataExtDO::getId, value));
    }

    default Long selectCountByParentId(Long parentId) {
        return selectCount(new LambdaQueryWrapper<DictDataExtDO>().eq(DictDataExtDO::getParentId, parentId));
    }

    default List<DictDataExtDO> selectList(DictDataExtListReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<DictDataExtDO>()
                .likeIfPresent(DictDataExtDO::getLabel, reqVO.getLabel())
                .eqIfPresent(DictDataExtDO::getStatus, reqVO.getStatus()));
    }
}